/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plot;
//////////////////////////////////////////////////////////////////////////
//                                                                      //
// Interface Java with Gnuplot                                          //
//                                                                      //
// Author:  Nicolas Pouvesle (nicolas@pouvesle.fr)                      //
// Created: December 5, 2007                                            //
// Free class released under the GNU General Public License             //
//                                                                      //
// Please feel free to send me any comments about this class            //
//                                                                      //
//////////////////////////////////////////////////////////////////////////
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;

/**
 *
 * @author andelin
 */
public class jgnuplot {

    Process p = null;
    BufferedReader stdInput = null;
    BufferedWriter stdOutput = null;
    BufferedReader stdError = null;

    /**
     * Default Constructor
     */
    public jgnuplot() {

        String s = null;

        try {

            // run gnuplot through a process         
            p = Runtime.getRuntime().exec("C:/Program Files/gnuplot/bin/wgnuplot.exe");

            // Get the Process standart input
            stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

            // Get the Process standart output
            stdOutput = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));

            // Get the Process standart output error
            stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

        } catch (IOException e) {
            System.out.println("exception happened - here's what I know: ");
            e.printStackTrace();
            System.exit(-1);
        }

    }

    /**
     * Send command to Gnuplot
     *
     * @param cmd  Gnuplot shell command.
     */
    public void cmd(String cmd) {

        try {
            stdOutput.write(cmd + "\n");
            stdOutput.flush();
        } catch (IOException e) {
            System.out.println("exception happened - here's what I know: ");
            e.printStackTrace();
            System.exit(-1);
        }
    }

    /**
     * Get Gnuplot current outputstream line.
     */
    public String getLine() {

        String aws = null;

        try {
            //if (stdError.ready()) {
            aws = stdError.readLine();
        //}
        } catch (IOException e) {
            System.out.println("exception happened - here's what I know: ");
            e.printStackTrace();
            System.exit(-1);
        }

        return aws;
    }

    /**
     * Get next no empty outputstream line.
     */
    public String getNoEmptyLine() {

        String aws = getLine();

        if (aws != null) {
            if (aws.equals("")) {
                aws = getNoEmptyLine();
            }
        }
        return aws;
    }

    /**
     * Get Gnuplot variable.
     *
     * @param var  Gnuplot variable name.
     */
    public String getVar(String var) {

        // Ask for the variable
        cmd("if (defined(" + var + ")) print " + var + "; else print \"None\"");

        // Get the answer
        String aws = getNoEmptyLine();

        // Test if the variable was valid
        if (aws.equals("None")) {
            aws = null;
        }

        return aws;
    }
}
